<?php
/**
 *  添加菜单功能
 */
if (function_exists('register_nav_menus')) {
	register_nav_menus(array('navbar-menu' => __('导航菜单'), 'footer-menu' => __('底部菜单'), 'links-menu' => __('友情链接')));
}

//~ 无菜单时调用
function call_menu() {
	echo '<div class="menu fl"><ul class="menu_ul"><li class="menu_li">' . __('<a target="_blank" href="/wp-admin/nav-menus.php">设置菜单</a>') . '</li></ul></div>';
}

//~ 导航菜单
function navbar_menu() {
	if (has_nav_menu('navbar-menu')) {
		wp_nav_menu(array('theme_location' => 'navbar-menu', 'depth' => 2, 'container_class' => 'menu fl', 'items_wrap' => '<ul class="menu_ul">%3$s</ul>', 'walker' => new menu_walker()));
	} else {
		call_menu();
	}
}

//~ 专题菜单
function footer_menu() {
	if (has_nav_menu('footer-menu')) {
		wp_nav_menu(array('theme_location' => 'footer-menu', 'depth' => 1, 'container_class' => 'menu fl', 'items_wrap' => '<ul class="menu_ul">%3$s</ul>', 'walker' => new menu_walker()));
	} else {
		call_menu();
	}
}

//~ 友情链接
function links_menu() {
	if (has_nav_menu('links-menu')) {
		wp_nav_menu(array('theme_location' => 'links-menu', 'depth' => 1, 'container_class' => 'menu fl', 'items_wrap' => '<ul class="menu_ul">%3$s</ul>', 'walker' => new menu_walker()));
	} else {
		call_menu();
	}
}

//~ 导航菜单自定义遍历
class menu_walker extends Walker_Nav_Menu {

	public function start_lvl(&$output, $depth = 0, $args = array()) {
		if ($depth === 0)
			$output .= '<ul class="menu_ul_ul">';
	}

	public function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
		if ($depth === 0) {

			$classes = empty($item -> classes) ? array() : (array)$item -> classes;

			$class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args));

			//~ 替换当前菜单样式
			$class_names = str_replace('current-category-ancestor ', '', $class_names);
			$class_names = str_replace('current-category-ancestor', '', $class_names);
			$class_names = str_replace('current-category-parent', '', $class_names);
			$class_names = str_replace('current-menu-item ', ' on', $class_names);
			$class_names = str_replace('current-menu-item', ' on', $class_names);
			$class_names = str_replace('current-menu-parent ', 'on', $class_names);
			$class_names = str_replace('current-menu-ancestor ', '', $class_names);
			$class_names = str_replace('current_page_parent ', '', $class_names);
			$class_names = str_replace('current_page_ancestor ', '', $class_names);
			$class_names = str_replace('current_page_item ', '', $class_names);
			$class_names = str_replace('menu-item ', '', $class_names);
			$class_names = str_replace('menu-item-object-page', '', $class_names);
			$class_names = str_replace('menu-item-object-post', '', $class_names);
			$class_names = str_replace('menu-item-object-custom ', '', $class_names);
			$class_names = str_replace('menu-item-object-category ', '', $class_names);
			$class_names = str_replace('menu-item-object-category', '', $class_names);
			$class_names = str_replace('menu-item-type-custom ', '', $class_names);
			$class_names = str_replace('menu-item-type-taxonomy ', '', $class_names);
			$class_names = str_replace('menu-item-type-post_type ', '', $class_names);
			$class_names = str_replace(' menu-item-has-children', '', $class_names);
			$class_names = str_replace('menu-item-has-children', '', $class_names);
			$class_names = str_replace('menu-item-home', ' menu_home', $class_names);
			$class_names = str_replace('current-post-ancestor', '', $class_names);
			$class_names = str_replace('soncurrent-post-parent', 'on', $class_names);
			$class_names = str_replace('oncurrent-post-parent', 'on', $class_names);
			$class_names = str_replace('fatherpage_item', 'on', $class_names);
			$class_names = str_replace('page_item', '', $class_names);

			$class_names = 'menu_li' ? 'menu_li' . $class_names : '';

		} elseif ($depth === 1)
			$class_names = 'menu_li_li';

		$output .= '<li class="' . $class_names . '">';
		$atts['href'] = !empty($item -> url) ? $item -> url : '';
		$atts = apply_filters('nav_menu_link_attributes', $atts, $item, $args);

		foreach ($atts as $attr => $value) {
			if (!empty($value)) {
				$value = ('href' === $attr) ? esc_url($value) : esc_attr($value);
				$attributes = ' ' . $attr . '="' . $value . '"';
			}
		}

		$item_output = '<a' . $attributes . '>';
		$item_output .= $args -> link_before . apply_filters('the_title', $item -> title, $item -> ID) . $args -> link_after;
		$item_output .= ($args -> has_children && 0 === $depth) ? '<b></b></a>' : '</a>';
		$output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
	}

	public function display_element($element, &$children_elements, $max_depth, $depth, $args, &$output) {
		if (!$element)
			return;

		$id_field = $this -> db_fields['id'];

		if (is_object($args[0]))
			$args[0] -> has_children = !empty($children_elements[$element -> $id_field]);

		parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
	}

}
